package com.greensecurity.sim.repository;

import com.greensecurity.sim.entity.Green;
import com.greensecurity.sim.entity.GreenLand;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import javax.transaction.Transactional;
import java.util.List;

@Repository
public interface GreenRepository extends JpaRepository<Green,Long> {
    @Query(value = "SELECT inter_num FROM green WHERE date = ?1 ORDER BY inter_num DESC LIMIT 1",nativeQuery = true)
    String getInterNun(String dateTime);

    @Query(value = "SELECT id FROM green WHERE inter_num = ?1",nativeQuery = true)
    long findByInterNum(String interNum);

    @Query(value = "SELECT * FROM green LIMIT 5",nativeQuery = true)
    List<Green> findGreenLimit();


    @Transactional//注解在代码执行出错的时候能够进行事务的回滚。
    @Modifying
    @Query(value = "DELETE FROM user_green WHERE green_id = ?1 ",nativeQuery = true)
    void deleteGreendKey(Long id);
}
